package com.jdew.thkj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.manager.ListDataSave;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.onitemInterface.OnItemDelectListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/22.
 */

public class HistorySearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private List<String> list;
    private Context context;
    protected LayoutInflater mInflater;
    private OnItemClickListener mItemClickListener;
    private OnItemDelectListener mDelectItemClickListener;
    private ListDataSave listsave;

    public HistorySearchAdapter(Context context, List<String> list) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        listsave = new ListDataSave(context, "history_record");
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.list_item, parent, false);
        itemView.setOnClickListener(this);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder myviewHolder = (ViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }
    public void setDelectItemClickListener(OnItemDelectListener mDelectItemClickListener) {
        this.mDelectItemClickListener = mDelectItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.clear)
        ImageView clear;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void BindView(RecyclerView.ViewHolder holder, final int position) {
            ((ViewHolder) holder).name.setText(list.get(position).replace("[","").replace("]","").replace("\\","").replace("\"",""));
            ((ViewHolder) holder).clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if(mDelectItemClickListener!=null){
                        mDelectItemClickListener.onDelectItemClick(position);
                    }
                }
            });
        }
    }
}
